//
//  YLRegularFinancingDetailViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/14.
//  Copyright © 2017年 she1323. All rights reserved.
//  定期理财详情

#import "YLRegularFinancingDetailViewModel.h"

#import "WHNetworkTool.h"
#import "YLAccountTool.h"
#import "WHProgressHUD.h"
#import "YLRegularFinancingDetailModel.h"
#import <YYModel.h>

@implementation YLRegularFinancingDetailViewModel

- (NSArray<YLTitleContentModel *> *)models
{
    if (!_models) {
        _models = [NSArray array];
    }
    return _models;
}


- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetOrderSingleInfo", YLURL];
                [WHProgressHUD showStatus:@"数据正在加载中..."];
                NSDictionary *param = @{@"ContractCode" : input};
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:param success:^(id responseObject) {
                    [WHProgressHUD dismiss];
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        YLRegularFinancingDetailModel *model = [YLRegularFinancingDetailModel yy_modelWithDictionary:responseObject[@"httpResult"]];
                        weakSelf.productName = model.productName;
                        weakSelf.models = @[[YLTitleContentModel modelWithTitle:@"本金" content:[NSString stringWithFormat:@"%.2f", model.investmentAmount]],
                                            [YLTitleContentModel modelWithTitle:@"期限" content:[NSString stringWithFormat:@"%@(月)", model.productTerm]],
                                            
                                            [YLTitleContentModel modelWithTitle:@"预期年化" content:[NSString stringWithFormat:@"%@%%", model.productAnnualizedYield]],
                                            
                                            [YLTitleContentModel modelWithTitle:@"预期收益" content:[NSString stringWithFormat:@"%.2f", model.incomeAmount]],
                                            
                                            [YLTitleContentModel modelWithTitle:@"返息方式" content: model.payCategoryText],
                                            
                                            [YLTitleContentModel modelWithTitle:@"收益起始日" content:model.calulationDate],
                                            
                                            [YLTitleContentModel modelWithTitle:@"收益到期日" content:model.endTime],
                                            
                                            [YLTitleContentModel modelWithTitle:@"加息利率" content:model.additionalInterestRate],
                                            
                                            [YLTitleContentModel modelWithTitle:@"满减金额" content:[NSString stringWithFormat:@"%.2f", model.additionalIncomeAmount]]];
                        
                        weakSelf.model = model;
                        [subscriber sendNext:nil];
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];

                return nil;
                
            }];
            }];
    }
    return _requestCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
    [WHProgressHUD dismiss];
}

@end
